<div class="modal-header">
    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
    <h4 class="modal-title">类别管理</h4>
</div>
<form id="defaultForm" method="post" class="form-horizontal" action="/project/indexManage/type_save">
    <div class="modal-body">
        <div class="form-group">
            <label class="col-lg-3 control-label">编号</label>
            <div class="col-lg-6">
                <input type="text" class="form-control" name="code" onchange="doVerify()"/>
            </div>
            <sanp id="accountText"></sanp>
        </div>

        <div class="form-group">
            <label class="col-lg-3 control-label">类别名称</label>
            <div class="col-lg-6">
                <input type="text" class="form-control" name="name" />
            </div>
        </div>


        <div class="form-group">
            <label class="col-lg-3 control-label">适用对象</label>
            <div class="col-lg-6">
                <b>项目:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</b><input type="checkbox" name="objName" value="1" onclick="aaa()"/>
                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                <b>电池包: </b><input type="checkbox" name="objName" value="2" onclick="aaa()"/>
                </br>
                <b>电池模组: </b><input type="checkbox" name="objName" value="3" onclick="aaa()"/>
                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                <b>变流器: </b><input type="checkbox" name="objName" value="4"  onclick="aaa()"/>
                </br>
                <b>电池单体: </b><input type="checkbox" name="objName" value="5" onclick="aaa()"/>
                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                <b>平台:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</b><input type="checkbox" name="objName" value="6" onclick="aaa()"/>
            </div>
        </div>

        <div class="form-group">
            <label class="col-lg-3 control-label">适用指标</label>
            <div class="col-lg-6">
                <b>参数测点: </b><input type="checkbox" name="useTarget" value="1" onclick="bbb()"/>
                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                <b>过程报警: </b><input type="checkbox" name="useTarget" value="2" onclick="bbb()"/>
                </br>
                <b>设备状态: </b><input type="checkbox" name="useTarget" value="3" onclick="bbb()"/>
                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                <b>台账属性: </b><input type="checkbox" name="useTarget" value="4"  onclick="bbb()"/>
                </br>
                <b>控制指令: </b><input type="checkbox" name="useTarget" value="5" onclick="bbb()"/>
                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                <b>阈值管理:  </b><input type="checkbox" name="useTarget" value="6" onclick="bbb()"/>
            </div>
        </div>


    </div>
    <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
        <button type="submit"  id = "saveBtn" class="btn btn-primary">保存</button>
    </div>
</form>

<script>
    $('#defaultForm').bootstrapValidator({
        message: 'This value is not valid',
        feedbackIcons: {
            valid: 'glyphicon glyphicon-ok',
            invalid: 'glyphicon glyphicon-remove',
            validating: 'glyphicon glyphicon-refresh'
        }, fields: {
            code: {
                validators: {
                    notEmpty: {
                        message: 'CODE不能为空'
                    }
                }
            },
            name: {
                validators: {
                    notEmpty: {
                        message: '名称不能为空'
                    }
                }
            }
        }
    }).on('success.form.bv', function (e) {
        // 阻止默认提交事件
        e.preventDefault();

        // 获取form表单
        var $form = $(e.target);

        // Get the BootstrapValidator instance
        var bv = $form.data('bootstrapValidator');
        console.log($('#defaultForm').serialize());

        $.ajax({
            url: $form.attr('action'),
            type: "post",
            data: $form.serialize(),
            dataType: 'json',
            success: function (result) {
                if (result.type == "alert") {
                    alert(result.message);
                }
                $("#modal").modal("hide");
                //刷新跳转至第一页
                // $("#dataTable").bootstrapTable("selectPage",1).bootstrapTable("refresh");
                $("#dataTable").bootstrapTable("selectPage", 1);
            },
            error: function () {
                console.log("error")
            }
        });
    });
    window.onload = function () {
        document.getElementById("defaultForm");
    }

    /*****************************************编号唯一验证******************************************/
    function doVerify() {

        var cod = $("input[name ='code']").val()
        var accountText = $("#accountText")[0];
        if (cod != "") {
            $.ajax({
                url: "/project/indexManage/type_code",//后台查询验证的方法
                data: {"code": cod},//携带的参数
                type: "post",
                success: function (data) {
                    //根据后台返回前台的msg给提示信息加HTML
                    if (data) {
                        // 已经存在
                        accountText.innerHTML = "<font color='red' size='3'>" + data + "</font>"
                        //  alert(cod);
                        $("#saveBtn").attr("disabled", true);

                    }
                    accountText.innerHTML = "<font color='red' size='3'>" + data + "</font>"
                }
            });
        } else {
            accountText.innerText = "";
        }

    }




function aaa() {
    var str = "";
    $("input[name='objName']:checked").each(function (i) {
        if (i == 0) {
            str = $(this).val();
        } else {
            str += "," + $(this).val();
        }
    })
}


function bbb() {
        var str = "";
        $("input[name='useTarget']:checked").each(function (i) {
            if (i == 0) {
                str = $(this).val();
            } else {
                str += "," + $(this).val();
            }
        })
    }

</script>